package edu.sctu.zhoudie.mouth201506.day20150625;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.*;

/**
 * Created by user08 on 2015/6/25.
 */
public class JsonUtil1{
    static HttpClientBuilder builder = HttpClientBuilder.create();
    static CloseableHttpClient client = builder.build();


    /**
     * get方法
     * @throws java.io.IOException
     */
    public static String GetJson(String path) throws IOException {
        HttpGet get = new HttpGet(path);
        HttpResponse response = client.execute(get);
        HttpEntity entity =response.getEntity();

        InputStream inputStream = entity.getContent();

        String str;
        str = steamToString(inputStream);


        get.releaseConnection();
        return str;
    }

    /**
     * json数据解析
     * @param
     * @return
     */
    public static MainData jsonAnalytical(String json){
        JSONObject jsonObject = JSON.parseObject(json);
        MainData mainData = MainData.getDateObj();

        mainData.error = jsonObject.getInteger("error");
        mainData.count = jsonObject.getString("count");
        mainData.message = jsonObject.getString("message");
        mainData.page = jsonObject.getString("page");
        mainData.size = jsonObject.getString("size");
        JSONArray list = jsonObject.getJSONArray("list");

        for (int i = 0; i < list.size(); i++) {
            Goods goods = new Goods();
            String str = list.getString(i);
            JSONObject jsonObject1 = JSON.parseObject(str);

            goods.goods_id = jsonObject1.getString("goods_id");
            goods.goods_name = jsonObject1.getString("goods_name");
            goods.goods_price = jsonObject1.getString("goods_price");

            mainData.list.add(goods);

        }


        return mainData;
    }


//    public static void serializableGoods(){
//        try {
//            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("D:/hah.txt")));
//            outputStream.writeObject(MainData.getDateObj());
//            outputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    /**
     * 流变为字符串
     * @param inputStream
     * @return
     * @throws java.io.IOException
     */
    public static String steamToString(InputStream inputStream) throws IOException {
        byte[] buffer = new  byte[1024];
        int count;
        String str = "     ";

        while((count = inputStream.read(buffer)) !=-1){                  //为什么必须循环4次才能读完
            str = str + new String(buffer,0,count,"utf-8");
        }

        return str;
    }

    public  static void printfJson(MainData mainData){

//        try {
//            MainData mainData = (MainData) new ObjectInputStream(new FileInputStream(new File("D:/hah.txt"))).readObject();
            System.out.println("error:"+mainData.error);
            System.out.println("size:"+mainData.size);
            System.out.println("page:"+mainData.page);
            System.out.println("message:"+mainData.message);
            System.out.println("count:"+mainData.count);

            System.out.println(mainData.list.size());
            for (int i = 0; i < mainData.list.size(); i++) {
                System.out.println("good"+i+":");
                System.out.println("goods_id:"+mainData.list.get(i).goods_id);
                System.out.println("goods_name:"+mainData.list.get(i).goods_name);
                System.out.println("goods_price:"+mainData.list.get(i).goods_price);
            }

//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
    }
}
